/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Img_PanelProd.java
 *
 * Created on 17/05/2010, 12:22:05 PM
 */

package administrador;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
 *
 * @author daniel1
 */
public class Img_PanelProd extends javax.swing.JPanel {
pedidoReposicion bi;
String bodega;
String codpedido="";
    /** Creates new form Img_PanelProd */
    public Img_PanelProd(pedidoReposicion bi,String bodega,String codpedido) {
        initComponents();
        this.bi=bi;
        this.bodega=bodega;
        this.codpedido=codpedido;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txt_nombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_descripcion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txt_cant = new javax.swing.JTextField();
        txt_precio = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        bt_guardar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lbl_imagen = new javax.swing.JLabel();
        lbl_codigo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txt_ped = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(Img_PanelProd.class);
        setForeground(resourceMap.getColor("Form.foreground")); // NOI18N
        setName("Form"); // NOI18N

        txt_nombre.setName("txt_nombre"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txt_descripcion.setName("txt_descripcion"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txt_cant.setFont(resourceMap.getFont("txt_cant.font")); // NOI18N
        txt_cant.setName("txt_cant"); // NOI18N

        txt_precio.setFont(resourceMap.getFont("txt_precio.font")); // NOI18N
        txt_precio.setForeground(resourceMap.getColor("txt_precio.foreground")); // NOI18N
        txt_precio.setName("txt_precio"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        bt_guardar.setText(resourceMap.getString("bt_guardar.text")); // NOI18N
        bt_guardar.setName("bt_guardar"); // NOI18N
        bt_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_guardarActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lbl_imagen.setText(resourceMap.getString("lbl_imagen.text")); // NOI18N
        lbl_imagen.setName("lbl_imagen"); // NOI18N
        jScrollPane1.setViewportView(lbl_imagen);

        lbl_codigo.setFont(resourceMap.getFont("lbl_codigo.font")); // NOI18N
        lbl_codigo.setForeground(resourceMap.getColor("lbl_codigo.foreground")); // NOI18N
        lbl_codigo.setText(resourceMap.getString("lbl_codigo.text")); // NOI18N
        lbl_codigo.setName("lbl_codigo"); // NOI18N
        lbl_codigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                lbl_codigoKeyTyped(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txt_ped.setText(resourceMap.getString("txt_ped.text")); // NOI18N
        txt_ped.setName("txt_ped"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                    .addComponent(lbl_codigo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3))
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_descripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txt_cant, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txt_precio, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txt_ped, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_guardar, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                            .addComponent(txt_nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txt_cant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txt_precio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_guardar)
                    .addComponent(jLabel1)
                    .addComponent(txt_ped, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bt_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_guardarActionPerformed
        // TODO add your handling code here:
        //creo el pedido de reposicion:
        ResultSet re=bi.conexion.consultarBase("SELECT * FROM itm_pedreposicion where cod_producto like '"+this.lbl_codigo.getText()+"' and cod_pedido="+this.codpedido+" ;");
        int fila=bi.conexion.obtenerFilas(re);
        boolean exito1=false;
        boolean exito2=false;

        if(fila>0){
        //existe

            exito1=bi.conexion.actualizarBaseResp("update itm_pedreposicion set cantidad="+this.txt_ped.getText()+" where cod_producto like '"+this.lbl_codigo.getText()+"' and cod_pedido="+this.codpedido+" ;");
            if(this.txt_ped.getText().equals("0")){
            bi.conexion.actualizarBaseResp("delete from itm_pedreposicion where cantidad=0;");
            }
            exito2=true;
        }else{
        //no existe
            exito2=bi.conexion.insertarDatosR("insert into itm_pedreposicion values("+this.codpedido+",'"+this.lbl_codigo.getText()+"','"+this.txt_nombre.getText()+"','"+this.txt_descripcion.getText()+"',"+this.txt_ped.getText()+","+this.txt_precio.getText()+");");
            exito1=true;
        }

        //Realizo la actualizacion y la insercion
        boolean exito=bi.conexion.actualizarBaseResp("update productosbodega set cant_pedida="+txt_ped.getText()+"  where codigo like '"+lbl_codigo.getText()+"' ;");

        if(exito&&exito1&&exito2){
         JOptionPane.showMessageDialog(this, "Se actualizo correctamente !!", "Informacion", JOptionPane.WARNING_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(this, "Se produjo un error en la actualizacion !!", "Informacion", JOptionPane.WARNING_MESSAGE);
        }


    }//GEN-LAST:event_bt_guardarActionPerformed

    private void lbl_codigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lbl_codigoKeyTyped
        // TODO add your handling code here:


    }//GEN-LAST:event_lbl_codigoKeyTyped


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_guardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JLabel lbl_codigo;
    public javax.swing.JLabel lbl_imagen;
    public javax.swing.JTextField txt_cant;
    public javax.swing.JTextField txt_descripcion;
    public javax.swing.JTextField txt_nombre;
    public javax.swing.JTextField txt_ped;
    public javax.swing.JTextField txt_precio;
    // End of variables declaration//GEN-END:variables

}
